package app.view

import groovy.transform.CompileStatic

import java.awt.BorderLayout
import java.awt.FlowLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener

import javax.swing.BoxLayout
import javax.swing.JButton
import javax.swing.JDialog
import javax.swing.JPanel

import model.agent.question.QuestionSet

@CompileStatic
class QuestionFrame extends JDialog{

	String questions = ""

	QuestionSet qs

	ArrayList optionPanels = []

	ArrayList<String> results = null

	def QuestionFrame(String questions){

		this.questions = questions
		
		this.title = "诊断问题"

		qs = new QuestionSet(questions)

		def mainPan=new JPanel(new BorderLayout())

		def buttonPan = new JPanel(new FlowLayout())

		mainPan.add(buttonPan,BorderLayout.SOUTH)

		def okButton = new JButton("OK")

		okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						results = new ArrayList<String>()
						def options =  optionPanels.collect{ op ->
							((LOptionsPanel)op).getSelectedOptions()
						}
						options.each{ subOptions ->
							subOptions.each{ option ->
								results.add((String)option)
							}
						}
						//setVisible(false);
						dispose();
					} // actionPerformed
				});

		buttonPan.add(okButton)

		def cancelButton = new JButton("Cancel")

		cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						results = null
						dispose();
					} // actionPerformed
				});

		buttonPan.add(cancelButton)

		def questionPan = new JPanel()
		questionPan.setLayout(new BoxLayout(questionPan,BoxLayout.Y_AXIS))

		mainPan.add(questionPan,BorderLayout.CENTER)

		qs.subQuestions.each{
			def op = new LOptionsPanel(it)
			optionPanels.add(op)
			questionPan.add(op)
		}

		this.add(mainPan)
	}

	ArrayList<String> showDialog(){

		this.modal = true
		this.setLocationRelativeTo(null)
		this.pack()
		this.setVisible(true)

		return results

	} // show

}
